package web.protocols.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import domain.entities.*;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import services.*;
import services.exceptions.NoProtocoloCompromisopaisSelectedException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Guillermo
 * Date: 11/09/12
 * Time: 10:17
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/Protocols/ProtocolLevelDoc")
public class ProtocolLevelDocumentationController extends ActionSupport implements ModelDriven {


    private ProtocolLevelDocumentationService protocolLevelDocumentationService;
    private ProtocolLevelDocumentationCategoryService documentationCategoryService;
    private ProtocoloCompromisopaisService protocoloCompromisopaisService;
    private TranslatorsCRUDService<Traductor> translatorsCRUDService;
    private ProtocolCRUDService protocolCRUDService;
    private CompromisoPaisService compromisoPaisService;

    private ProtocolLevelDocumentation protocolLevelDocumentation = new ProtocolLevelDocumentation();
    private List<ProtocolLevelDocumentationCategory> categories;
    private List<Compromisopais> compromisopaisList = new ArrayList<Compromisopais>();
    private List<String> compromisopaisListSelect = new ArrayList<String>();
    private List<Traductor> traductors;
    private List<String> translationRate;

    private List<ProtocolLevelDocumentation> list;
    private Integer protocolLevelDocumentationId;
    private Integer hdTranslatorReviewerID;
    private Integer hdTranslatorID;
    private String translatorReviewer;
    private String translator;


    @Autowired
    public ProtocolLevelDocumentationController(ProtocolLevelDocumentationService protocolLevelDocumentationService,
                                                ProtocolLevelDocumentationCategoryService documentationCategoryService,
                                                ProtocoloCompromisopaisService protocoloCompromisopaisService, ProtocolCRUDService protocolCRUDService,
                                                TranslatorsCRUDService<Traductor> translatorsCRUDService,
                                                CompromisoPaisService compromisoPaisService) {
        this.protocolLevelDocumentationService = protocolLevelDocumentationService;
        this.protocoloCompromisopaisService = protocoloCompromisopaisService;
        this.protocolCRUDService = protocolCRUDService;
        this.translatorsCRUDService = translatorsCRUDService;
        this.documentationCategoryService = documentationCategoryService;
        this.compromisoPaisService = compromisoPaisService;
    }

    @Override
    public Object getModel() {
        return protocolLevelDocumentation;
    }


    @Action(value="main", results={
            @Result(name="success",location="/directories.documents_protocols.tiles", type="tiles")
    })
    public String execute() {

        ProtocoloCompromisopais aux = null;
        try {
            aux = protocolCRUDService.recoverProtocoloCompromisopais();
        } catch (NoProtocoloCompromisopaisSelectedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        if(aux == null)
            return null;
        String protocoloId = aux.getProtocolo().getId();

        list = protocolLevelDocumentationService.findByProtocoloId(aux.getProtocolo().getId(), aux.getCompromisopais().getId());

        categories = documentationCategoryService.listAll();
        ProtocolLevelDocumentationCategory v = new ProtocolLevelDocumentationCategory( -2, "Other");
        categories.add(v);

        traductors = translatorsCRUDService.findAll();
        translationRate = new ArrayList<String>();
        translationRate.add("Not acceptable");
        translationRate.add("Regular");
        translationRate.add("Good");
        translationRate.add("Very Good");
        translationRate.add("Excelent");

        compromisopaisList = new ArrayList<Compromisopais>();
        for(ProtocoloCompromisopais c : aux.getProtocolo().getProtocoloCompromisopaises()){
            compromisopaisList.add(c.getCompromisopais());
        }

        if(protocolLevelDocumentationId != null){
            protocolLevelDocumentation =    protocolLevelDocumentationService.findById(protocolLevelDocumentationId);

            for(ProtocoloCompromisopais p : protocolLevelDocumentation.getProtocoloCompromisopaises()){
                compromisopaisListSelect.add( p.getCompromisopais().getCountry() );
            }
            if( protocolLevelDocumentation.getTraductorReviewer() != null)   {
                translatorReviewer = protocolLevelDocumentation.getTraductorReviewer().getSurname() + ", " +protocolLevelDocumentation.getTraductorReviewer().getName();
                hdTranslatorReviewerID = protocolLevelDocumentation.getTraductorReviewer().getId();
            }
            if( protocolLevelDocumentation.getTraductor() != null){
                translator = protocolLevelDocumentation.getTraductor().getSurname() + ", " +protocolLevelDocumentation.getTraductor().getName();
                hdTranslatorID = protocolLevelDocumentation.getTraductor().getId();
            }
        } else{
            compromisopaisListSelect.clear();
            compromisopaisListSelect.add(aux.getCompromisopais().getCountry());
            protocolLevelDocumentation = new ProtocolLevelDocumentation();
            hdTranslatorID = null;
            hdTranslatorReviewerID = null;
            translator = "";
            translatorReviewer = "";
        }

        return SUCCESS;
    }

    @Action(value="save", results={
            @Result(name="success",location="main", type="redirectAction")
    })
    public String save() {

        String protocoloId = null;
        ProtocoloCompromisopais protocoloCompromisopais = null;
        try {
            protocoloCompromisopais = protocolCRUDService.recoverProtocoloCompromisopais();
            protocoloId = protocolCRUDService.recoverProtocoloCompromisopais().getProtocolo().getId();
        } catch (NoProtocoloCompromisopaisSelectedException e) {
            e.printStackTrace();
        }
        if(protocoloCompromisopais == null)
            return null;

        documentationCategoryService.checkCategory(protocolLevelDocumentation.getProtocolLevelDocumentationCategory().getDescripcion());

        if( !protocolLevelDocumentation.getProtocolLevelDocumentationCategory().getId().equals(-2) )
            protocolLevelDocumentation.setProtocolLevelDocumentationCategory(documentationCategoryService.findById(protocolLevelDocumentation.getProtocolLevelDocumentationCategory().getId()));
        else
            protocolLevelDocumentation.setProtocolLevelDocumentationCategory(documentationCategoryService.findByDescripcion(protocolLevelDocumentation.getProtocolLevelDocumentationCategory().getDescripcion()));

        if(hdTranslatorID != null && !translator.equals(""))
            protocolLevelDocumentation.setTraductor(translatorsCRUDService.findById(hdTranslatorID));
        else
            protocolLevelDocumentation.setTraductor(null);

        if(hdTranslatorReviewerID != null && !translatorReviewer.equals(""))
            protocolLevelDocumentation.setTraductorReviewer(translatorsCRUDService.findById(hdTranslatorReviewerID));
        else
            protocolLevelDocumentation.setTraductorReviewer(null);



         if(protocolLevelDocumentation.getId() != null) {
             ProtocolLevelDocumentation delete = protocolLevelDocumentationService.findById(protocolLevelDocumentation.getId());
             protocolLevelDocumentationService.delete(delete);
             protocolLevelDocumentation.setId(null);
        }


        protocolLevelDocumentation.setProtocolLevelDocumentationCategory(documentationCategoryService.findById(protocolLevelDocumentation.getProtocolLevelDocumentationCategory().getId()));

        List<ProtocoloCompromisopais> list = new ArrayList<ProtocoloCompromisopais>();

        for(int i=0; i<compromisopaisListSelect.size();i++){

            ProtocoloCompromisopais pcp = protocoloCompromisopaisService.getByProtocoloIdCompromisopaisId(protocoloId, compromisoPaisService.findByCountry(compromisopaisListSelect.get(i)).getId()) ;
            list.add(pcp);

        }

        protocolLevelDocumentation.setProtocoloCompromisopaises(list);

        protocolLevelDocumentationService.save(protocolLevelDocumentation);


        return SUCCESS;
    }


    public ProtocolLevelDocumentation getProtocolLevelDocumentation() {
        return protocolLevelDocumentation;
    }

    public void setProtocolLevelDocumentation(ProtocolLevelDocumentation protocolLevelDocumentation) {
        this.protocolLevelDocumentation = protocolLevelDocumentation;
    }

    public List<String> getCompromisopaisListSelect() {
        return compromisopaisListSelect;
    }

    public void setCompromisopaisListSelect(List<String> compromisopaisListSelect) {
        this.compromisopaisListSelect = compromisopaisListSelect;
    }

    public List<Compromisopais> getCompromisopaisList() {
        return compromisopaisList;
    }

    public void setCompromisopaisList(List<Compromisopais> compromisopaisList) {
        this.compromisopaisList = compromisopaisList;
    }

    public List<ProtocolLevelDocumentationCategory> getCategories() {
        return categories;
    }

    public void setCategories(List<ProtocolLevelDocumentationCategory> categories) {
        this.categories = categories;
    }

    public List<Traductor> getTraductors() {
        return traductors;
    }

    public void setTraductors(List<Traductor> traductors) {
        this.traductors = traductors;
    }

    public List<String> getTranslationRate() {
        return translationRate;
    }

    public void setTranslationRate(List<String> translationRate) {
        this.translationRate = translationRate;
    }

    public List<ProtocolLevelDocumentation> getList() {
        return list;
    }

    public void setList(List<ProtocolLevelDocumentation> list) {
        this.list = list;
    }

    public Integer getProtocolLevelDocumentationId() {
        return protocolLevelDocumentationId;
    }

    public void setProtocolLevelDocumentationId(Integer protocolLevelDocumentationId) {
        this.protocolLevelDocumentationId = protocolLevelDocumentationId;
    }

    public Integer getHdTranslatorID() {
        return hdTranslatorID;
    }

    public void setHdTranslatorID(Integer hdTranslatorID) {
        this.hdTranslatorID = hdTranslatorID;
    }

    public Integer getHdTranslatorReviewerID() {
        return hdTranslatorReviewerID;
    }

    public void setHdTranslatorReviewerID(Integer hdTranslatorReviewerID) {
        this.hdTranslatorReviewerID = hdTranslatorReviewerID;
    }

    public String getTranslatorReviewer() {
        return translatorReviewer;
    }

    public void setTranslatorReviewer(String translatorReviewer) {
        this.translatorReviewer = translatorReviewer;
    }

    public String getTranslator() {
        return translator;
    }

    public void setTranslator(String translator) {
        this.translator = translator;
    }
}
